package com.darwin.darwinUI.general
{
	import com.darwin.darwinNameSpace.darwin;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.manager.UIManager;
	
	import flash.display.DisplayObject;
	import flash.filters.BitmapFilter;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;

	/**
	 *	 样式文本框生成工厂类
	 * <BR/>使用时,先调用一次registerStyle方法注册样式方案,
	 * <BR/>随后在想要添加文本区的地方调用getTextField方法就可以获得自动设置好替换文本和所有属性的TextField
	 * @author Roland
	 * 
	 */
	public class TextFieldFactory
	{
		private static var _instance:TextFieldFactory;

		public function TextFieldFactory()
		{
			if (_instance == null)
			{
				super();
				_instance=this;
			}
			else
			{
				throw new Error("实例化单例类出错-TextFieldFactory");
			}
		}
		
		public static function getInstance():TextFieldFactory
		{
			if (_instance == null)
				_instance=new TextFieldFactory();
			
			return _instance;
		}
		
		private var _styleClass:Class;
		
		private var _stytleInstance:DisplayObject
		
		private var _styleDic:Dictionary;
		
		private var _fontName:String;
		
		private var _fontEmbedType:String;
		
		private var _styleXml:XML;

		/**
		 * 注册样式方案
		 * @param styleClass 样式方案对应的导出类
		 * @param styleRuleXml 样式规则对应的配置表
		 * @param fontName 嵌入字体的字体名
		 * @param fontEmbedType 字体嵌入类别
		 * 
		 */
		public function registerStyle(styleClass:Class,styleRuleXml:XML,fontName:String,fontEmbedType:String):void
		{
			_styleClass = styleClass;
			_stytleInstance = new _styleClass();
			_styleXml = styleRuleXml;
			_fontName = fontName;
			_fontEmbedType = fontEmbedType;
			
		}
		
		/**
		 *	取得一个文本区
		 * @param resourceName 文字索引，如果库中找不到这个索引则直接使用resourceName
		 * @param parameters 替换的参数，用于替换{}中的内容
		 * @return 
		 */
		public function getTextField(resourceName:String,parameters:Array = null):TextField
		{
			var textField:TextField;
			var styleName:String = getStyleTextFieldName(resourceName);
			var useEmbed:Boolean = getUseEmbed(resourceName);
			var contentStr:String = UIManager.getLanguageManager().getText(resourceName,parameters);
			textField = copyTextField(_stytleInstance[styleName],useEmbed,_fontName);
			textField.selectable = false;
			if(textField)
			{
				textField.htmlText = contentStr;
			}
			textField.x = textField.y = 0;
			return textField;
		}
		
		
		/**
		 *	取得一个文本区(返回Label)
		 * @param resourceName 文字索引，如果库中找不到这个索引则直接使用resourceName
		 * @param parameters 替换的参数，用于替换{}中的内容
		 * @return 返回设置好的Label，如果取不到样式将返回null
		 */
		public function getLabel(resourceName:String,parameters:Array = null):Label
		{
			var label:Label = new Label();
			var textField:TextField;
			var styleName:String = getStyleTextFieldName(resourceName);
			var useEmbed:Boolean = getUseEmbed(resourceName);
			var contentStr:String = UIManager.getLanguageManager().getText(resourceName,parameters);
			if(_stytleInstance[styleName] == null)return new Label(resourceName);
			textField = copyTextField(_stytleInstance[styleName],useEmbed,_fontName);
			textField.selectable = false;
			if(textField)
			{
				textField.htmlText = contentStr;
			}
			textField.x = textField.y = 0;
			use namespace darwin;
			label.textField = textField;
			return label;
		}
		
		/**
		 *	 根据resourceName取得注册的样式方案中对应TextField的实例名
		 * @param resourceName 
		 * @return 
		 * 
		 */
		private function getStyleTextFieldName(resourceName:String):String
		{
			var list:XMLList = _styleXml.item.(@key==resourceName);
			return list[0] ? list[0].@style : "";
		}
		
		/**
		 *	 根据resourceName取得注册的样式方案中对应TextField中文本是否使用嵌入字体
		 * @param resourceName
		 * @return 
		 * 
		 */
		private function getUseEmbed(resourceName:String):Boolean
		{
			var list:XMLList = _styleXml.item.(@key==resourceName);
			var useEmbedStr:String = list[0] ? list[0].@useEmbed : "0";
			if(useEmbedStr=="1")return true;//嵌入
			if(useEmbedStr=="2")return true;//可能嵌入
			return false;//不嵌入
		}
		
		/**
		 *	拷贝一个 textField 
		 * @param textField 要拷贝textField
		 * @param useEmbed 是否使用嵌入字体来覆盖原始textField中的字体
		 * @param embedFont 嵌入字体名
		 * @return 
		 * 
		 */
		public static function copyTextField(textField:TextField,useEmbed:Boolean = false,embedFont:String = ""):TextField
		{
			var $textField:TextField = new TextField();
			var $textFormat:TextFormat = textField.defaultTextFormat;
			var $filters:Array = [];
			for(var i:int=0;i<textField.filters.length;i++)
			{
				$filters.push(textField.filters[i].clone());
			}
			if(useEmbed)
			{
				$textFormat.font = embedFont;
				$textField.embedFonts = true;
			}
			$textField.defaultTextFormat = $textFormat;
			$textField.transform = textField.transform;
			$textField.filters = $filters;
			$textField.multiline = textField.multiline;
			textField.setTextFormat($textFormat);
			return $textField;
		}
	}
}